package kr.co.doriclassic.chapter18;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel_JTable extends JPanel implements ActionListener {
	private JTable table;
	private JButton btnAdd;

	/**
	 * Create the panel.
	 */
	public Panel_JTable() {

		initialize();
		tableCellAlginAndWidth();
	}
	
	private void tableCellAlginAndWidth() {
		//가운데 정렬
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		//우측 정렬
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel cModel = table.getColumnModel();
		
		int columnCount = cModel.getColumnCount();//컬럼의 개수
		
		cModel.getColumn(0).setPreferredWidth(70);
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(1).setPreferredWidth(70);
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(2).setPreferredWidth(70);
		cModel.getColumn(2).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(3).setPreferredWidth(70);
		cModel.getColumn(3).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(4).setPreferredWidth(150);
		cModel.getColumn(4).setCellRenderer(dtcrRight);
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_Top = new JPanel();
		add(panel_Top, BorderLayout.NORTH);
		
		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		panel_Top.add(btnAdd);
		
		JPanel panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		final JLabel lblSelected = new JLabel("New label");
		panel_Bottom.add(lblSelected);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		String[] colNames={"사원번호", "사원명", "직책", "매니저", "급여", "부서번호"};
		TableModel model = new DefaultTableModel(colNames, 0){
			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}
		};
		
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				int row = table.getSelectedRow();
				/*int col = table.getSelectedColumn();
				try{
					String text = (String) table.getModel().getValueAt(row, col);
					lblSelected.setText(text);
				}catch(ClassCastException e1){
					int value = (int) table.getModel().getValueAt(row, col);
					lblSelected.setText(value+"");
				}*/
				StringBuilder sb = new StringBuilder();
				for(int i=0; i<table.getColumnCount(); i++){
					sb.append(table.getModel().getValueAt(row, i)+" ");
				}
				lblSelected.setText(sb.toString());
			}
			
		});
		
		scrollPane.setViewportView(table);
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	protected void actionPerformedBtnAdd(ActionEvent e) {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		
		Object[] newRow = {
				new Integer( (int)(Math.random()*100)+1), 
				"김태희",
				"사원",
				new Integer( (int)(Math.random()*100)+1),
				new Integer( (int)(Math.random()*100000)+1),
				new Integer( (int)(Math.random()*10)+1)
		};
		
		model.addRow(newRow);
		
	}
}
